import 'package:flutter/material.dart';

import '../model/model_user_word.dart';
import '../service/service_user_word.dart';
import '../widget/widget_user_word.dart';

class PanelUserWordList extends StatefulWidget {
  const PanelUserWordList({super.key});

  @override
  State<PanelUserWordList> createState() => _PanelUserWordListState();
}

class _PanelUserWordListState extends State<PanelUserWordList> with AutomaticKeepAliveClientMixin {
  int _showType = 0;

  List<ModelUserWord> _data = [];

  int _allQty = 0;
  int _newQty = 0;
  int _reviewQty = 0;

  @override
  void initState() {
    super.initState();

    refreshData();
  }

  @override
  Widget build(BuildContext context) {
    // List<int> infos = DAOWord.countWord();
    // print(infos);

    return Container(
        child: Column(
          children: [buildTab(), buildList()],
        ));
  }

  @override
  bool get wantKeepAlive => true;

  refreshData() async {
    _allQty = await ServiceUserWord.countAll();
    _newQty = await ServiceUserWord.countNew();
    _reviewQty = await ServiceUserWord.countNeedReview();
    if (_showType == 0) {
      _data = await ServiceUserWord.listAll();
    } else if (_showType == 1) {
      _data = await ServiceUserWord.listNew();
    } else if (_showType == 2) {
      _data = await ServiceUserWord.listNeedReview();
    }
    setState(() {

    });
  }

  buildTab() {
    return Container(
      height: 50,
      child: Row(
        children: [
          Container(
            color: _showType == 0 ? Colors.lightBlueAccent : Colors.white,
            child: TextButton(
                onPressed: () {
                  _showType = 0;
                  refreshData();
                  setState(() {});
                },
                child: Text(
                  "所有(${_allQty})",
                )),
          ),
          Container(
            color: _showType == 1 ? Colors.lightBlueAccent : Colors.white,
            child: TextButton(
                onPressed: () {
                  _showType = 1;
                  refreshData();
                  setState(() {});
                },
                child: Text("所有生词(${_newQty})")),
          ),
          Container(
            color: _showType == 2 ? Colors.lightBlueAccent : Colors.white,
            child: TextButton(
                onPressed: () {
                  _showType = 2;
                  refreshData();
                  setState(() {});
                },
                child: Text("待复习(${_reviewQty})")),
          ),
        ],
      ),
    );
  }

  buildList() {
    return Expanded(
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Container(
          // color: Colors.red,

          child: ListView.builder(
              itemCount: _data.length,
              itemExtent: 80.0, //强制高度为50.0
              itemBuilder: (BuildContext context, int index) {
                String text = _data[index].word!;
                // return ListTile(title: Text(text));
                return WidgetUserWord(w:_data[index]);
              }),
        ),
      ),
    );
  }

}
